//
//  LFComposeView.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/26.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFComposeView.h"

@interface LFComposeView ()

@property (nonatomic,strong) UIViewController *target;

@property (nonatomic,strong) NSArray *buttons;

@end

@implementation LFComposeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}
#pragma mark 初始化方法
- (void)setUpUI
{
    //添加下面的关闭页面按钮
    UIButton *closeBtn = [[UIButton alloc] init];
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_close"] forState:UIControlStateNormal];
    closeBtn.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
    [closeBtn sizeToFit];
    [self addSubview:closeBtn];
    //设置自动布局
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(40);
    }];
    //依次添加两个按钮
    [self addButtons];
    //设置布局
    CGFloat offSetY = [UIScreen mainScreen].bounds.size.height*0.5;
    for (int i = 0; i < self.buttons.count; i++)
    {
        [UIView animateWithDuration:0.25 delay:0.1*(i+1) usingSpringWithDamping:1 initialSpringVelocity:0.7 options:0 animations:^{
            UIButton *button = self.buttons[i];
            button.y = offSetY-i*100;
        } completion:^(BOOL finished) {
            
        }];
        
    }

    
    
}
#pragma mark - 添加按钮的方法
- (void)addButtons
{
    
    CGFloat centerX = [UIScreen mainScreen].bounds.size.width*0.5;
    NSArray *array = @[@"weibo",@"transfer"];
    NSMutableArray *tempArray = [NSMutableArray array];
    for (int i = 0; i < 2; i++)
    {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(centerX-30, -100, 40, 40)];
        button.tag = i;
        [button setImage:[UIImage imageNamed:array[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [button sizeToFit];
        [self addSubview:button];
        [tempArray addObject:button];
    }
    self.buttons = tempArray;

}
//按钮点击的方法
- (void)btnClick:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        NSLog(@"------%zd",sender.tag);
        
    }else{
        if (self.clickBlock) {
            self.clickBlock();
        }
        
    }
}

#pragma mark - 显示当前控件
-(void)show:(UIViewController *)target
{
    self.target = target;
    [self.target.view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.offset(0);
    }];
}


#pragma mark - 关闭按钮的方法
- (void)closeBtnClick
{
    //设置布局
    for (int i = 0; i < self.buttons.count; i++)
    {
        [UIView animateWithDuration:0.25 delay:0.1*(i+1) usingSpringWithDamping:1 initialSpringVelocity:0.7 options:0 animations:^{
            UIButton *button = self.buttons[i];
            button.y = -100;
        } completion:^(BOOL finished) {
            if (i == self.buttons.count-1)
            {
                [self removeFromSuperview];
            }
        }];
        
    }
    
    
    
}

@end
